<?php
require_once('classes/ticket.php');
require_once('lib/nusoap.php');
require_once('lib/calcDateTime.class.php');

class Corendon{

	private $search;
	private $client;
	private $tickets = array();

	
	public function Corendon($searchObj){
		$this->search = $searchObj;
	}
	
	public function getTickets($index){
		
		$this->client = new  nusoap_client('https://secure.corendon.com/BlankToWebFare/Flight.asmx?WSDL', true);
		$xmlTemplate = $this->getTemplate();
		
		$err = $this->client->getError();
		$result = $this->client->call('GetAvailableFares', $xmlTemplate);
	
		//$stops =  count($result['GetAvailableFaresResult']['RSGetAvailableFares']['FLIGHTSOUT']) - 1;

		if($result["GetAvailableFaresResult"] != 0 ){
		if($stops == 0){
			$departureTime = new DateTime($result['GetAvailableFaresResult']['RSGetAvailableFares']['FLIGHTSOUT']['FLIGHT_STRC']['SEGMENTS']['FSEGMENT_STRC']['DEPARTUREDATE'] . $result['GetAvailableFaresResult']['RSGetAvailableFares']['FLIGHTSOUT']['FLIGHT_STRC']['SEGMENTS']['FSEGMENT_STRC']['DEPARTURETIME']);
			$arrivalTime = new DateTime($result['GetAvailableFaresResult']['RSGetAvailableFares']['FLIGHTSOUT']['FLIGHT_STRC']['SEGMENTS']['FSEGMENT_STRC']['ARRIVALDATE'] . $result['GetAvailableFaresResult']['RSGetAvailableFares']['FLIGHTSOUT']['FLIGHT_STRC']['SEGMENTS']['FSEGMENT_STRC']['ARRIVALTIME']);
			$interval = $departureTime->diff($arrivalTime);
			$interval = $interval->format('%H:%i');
			$stops = "0";
		}
					/*************************************************** KLASSE MAAKT MAAR 1 TICKET ******************************************************************/
			/***************************************************NAAM WORDT ALS ARRAY KEY GEBGRUIKT ******************************************************************/
	/*******************************LOOP GEBRUIKEN VOOR ALLE TICKETS, CORENDON GEEFT GEEN TUSSENSTOPS TERUG?!****************************************/
		foreach($result['GetAvailableFaresResult']['RSGetAvailableFares'] as $flight){
			$ticket = new Ticket($index,
				"http://www.corendon.com",
				(double)$result['GetAvailableFaresResult']['RSGetAvailableFares']['FAREADT'] * $this->search->totalPassengers(),
				(double)$result['GetAvailableFaresResult']['RSGetAvailableFares']['FAREADT'] ,
				(string)$result['GetAvailableFaresResult']['RSGetAvailableFares']['CARRIERCODE'],
				(double)$result['GetAvailableFaresResult']['RSGetAvailableFares']['TAXADT'],
				$stops,
				$interval
			);
				// $ticket = new Ticket((string)$flight->select_url,
								  // (double)$flight->total_price,
								  // (double)$flight->total_price / $this->search->totalPassengers(),
								  // (string)$flight->operating_carrier,
								  // (double)$flight->total_ticketfee,
								   // $stops,
								   // $journeyTime);
							  
		
		}
		foreach($result['GetAvailableFaresResult']['RSGetAvailableFares']['FLIGHTSOUT']['FLIGHT_STRC']['SEGMENTS'] as $outbound){
					
				//	print_r($outbound);
					
			
					 $departureDate = new DateTime($outbound['DEPARTUREDATE'] . $outbound['DEPARTURETIME']);
					 $arrivalDate = new DateTime($outbound['ARRIVALDATE'] .$outbound['ARRIVALTIME']);	
				

					 $interval = $departureDate->diff($arrivalDate);
					 $interval = $interval->format('%H:%i');					
					 
					$ticket->setFlightOutbound((string)$outbound['DEPARTURECODE'],
											   (string)$outbound['ARRIVALCODE'],
											   $departureDate,
											   $arrivalDate,
											   (string)$outbound['CARRIERCODE'],
											   $interval,
											   (string)$outbound['FLIGHTNUMBER']);
											  
		}
		if(isset($result['GetAvailableFaresResult']['RSGetAvailableFares']['FLIGHTSIN']['FLIGHT_STRC'])){
			  
			  foreach($result['GetAvailableFaresResult']['RSGetAvailableFares']['FLIGHTSIN']['FLIGHT_STRC']['SEGMENTS']  as $inbound){

				// echo '<h2>Result</h2><pre>';
				// print_r($inbound);
				// echo '</pre>';
				 $departureDate = new DateTime($inbound['DEPARTUREDATE'] . $inbound['DEPARTURETIME']);
				 $arrivalDate = new DateTime($inbound['SEGMENTS']['FSEGMENT_STRC']['ARRIVALDATE'] .$inbound['ARRIVALDATE']);	
				 $interval = $departureTime->diff($arrivalTime);
				 $interval = $interval->format('%H:%i');					
				 
				$ticket->setFlightInbound((string)$inbound['DEPARTURECODE'],
										   (string)$inbound['ARRIVALCODE'],
										   $departureDate,
										   $arrivalDate,
										   (string)$inbound['CARRIERCODE'],
										   $interval,
										   (string)$inbound['FLIGHTNUMBER']);
										  
			
			}
		}
	
		$this->tickets[$index] = $ticket;
		$index = $index + 1;

		}
		return $this->tickets;
	}
	

	
	
	
	private function getTemplate(){
		$deptDate = str_replace("-", "",$this->search->getDepartureDate()); 
		
		$xml =	 '
		<tem:GetAvailableFares xmlns:tem="http://tempuri.org/">
			<tem:request>
			   <tem:NUMBEROFADULTS>'. $this->search->getAdults().'</tem:NUMBEROFADULTS>
			   <tem:NUMBEROFCHILDREN>'. $this->search->getChildren().'</tem:NUMBEROFCHILDREN>
			   <tem:NUMBEROFINFANTS>'. $this->search->getInfants().'</tem:NUMBEROFINFANTS>
			   <!--Optional:-->
			   <tem:CARRIERCODES>
				  <!--Zero or more repetitions:-->
				  <tem:CARRIERCODE_STRC>
					 <!--Optional:-->
					 <tem:CODE></tem:CODE>
				  </tem:CARRIERCODE_STRC>
			   </tem:CARRIERCODES>
			   <!--Optional:-->
			   <tem:LANGUAGECODE>EN</tem:LANGUAGECODE>
			   <!--Optional:-->
			   <tem:MULTIDEPARTURECODESOUT>
				  <!--Zero or more repetitions:-->
				  <tem:AIRPORTCODE_STRC>
					 <!--Optional:-->
					 <tem:CODE>'.$this->search->getDeparturyCityIATA().'</tem:CODE>
				  </tem:AIRPORTCODE_STRC>
			   </tem:MULTIDEPARTURECODESOUT>
			   <!--Optional:-->
			   <tem:MULTIARRIVALCODESOUT>
				  <!--Zero or more repetitions:-->
				  <tem:AIRPORTCODE_STRC>
					 <!--Optional:-->
					 <tem:CODE>'.$this->search->getDestinationCityIATA().'</tem:CODE>
				  </tem:AIRPORTCODE_STRC>
			   </tem:MULTIARRIVALCODESOUT>
			   <!--Optional:-->
			   <tem:DEPARTUREDATEOUT>'.$deptDate.'</tem:DEPARTUREDATEOUT>
			   <!--Optional:-->
			   <tem:DEPARTURETIMEOUT></tem:DEPARTURETIMEOUT>
			   <tem:CHECKAHEADOUT>0</tem:CHECKAHEADOUT>
			   <tem:CHECKBACKOUT>0</tem:CHECKBACKOUT>';
			   
			   if($this->search-getRetour){
			   $retDate = str_replace("-", "",$this->search->getReturnDate()); 
				$xml = $xml . '
				   <!--Optional:-->
				   <tem:MULTIDEPARTURECODESIN>
					  <!--Zero or more repetitions:-->
					  <tem:AIRPORTCODE_STRC>
						 <!--Optional:-->
						 <tem:CODE>'.$this->search->getDestinationCityIATA().'</tem:CODE>
					  </tem:AIRPORTCODE_STRC>
				   </tem:MULTIDEPARTURECODESIN>
				   <!--Optional:-->
				   <tem:MULTIARRIVALCODESIN>
					  <!--Zero or more repetitions:-->
					  <tem:AIRPORTCODE_STRC>
						 <!--Optional:-->
						 <tem:CODE>'.$this->search->getDeparturyCityIATA().'</tem:CODE>
					  </tem:AIRPORTCODE_STRC>
				   </tem:MULTIARRIVALCODESIN>
				   <!--Optional:-->
				   <tem:DEPARTUREDATEIN>'.$retDate.'</tem:DEPARTUREDATEIN>
				   <!--Optional:-->
				   <tem:DEPARTURETIMEIN>0</tem:DEPARTURETIMEIN>
				   <tem:CHECKAHEADIN>0</tem:CHECKAHEADIN>
				   <tem:CHECKBACKIN>0</tem:CHECKBACKIN>';
			   }
			   $xml = $xml . '
			   <!--Optional:-->
			   <tem:FAREIDENTIFIER></tem:FAREIDENTIFIER>
			   <!--Optional:-->
			   <tem:AGENTS>
				  <!--Zero or more repetitions:-->
				  <tem:AGENT_STRC>
					 <!--Optional:-->
					 <tem:AGENT_ID></tem:AGENT_ID>
					 <!--Optional:-->
					 <tem:AGENT_PWD></tem:AGENT_PWD>
				  </tem:AGENT_STRC>
			   </tem:AGENTS>
			   <!--Optional:-->
			   <tem:EXCLUDECARRIERCODE>
				  <!--Zero or more repetitions:-->
				  <tem:CARRIERCODE_STRC>
					 <!--Optional:-->
					 <tem:CODE>0</tem:CODE>
				  </tem:CARRIERCODE_STRC>
			   </tem:EXCLUDECARRIERCODE>
			   <tem:TIMEWINDOWOUT>0</tem:TIMEWINDOWOUT>
			   <tem:TIMEWINDOWIN>0</tem:TIMEWINDOWIN>
			   <!--Optional:-->
			   <tem:ARRIVALDATEOUT></tem:ARRIVALDATEOUT>
			   <!--Optional:-->
			   <tem:ARRIVALTIMEOUT>0</tem:ARRIVALTIMEOUT>
			   <!--Optional:-->
			   <tem:ARRIVALDATEIN></tem:ARRIVALDATEIN>
			   <!--Optional:-->
			   <tem:ARRIVALTIMEIN></tem:ARRIVALTIMEIN>
			   <tem:NUMBEROFFARES>0</tem:NUMBEROFFARES>
			</tem:request>
		 </tem:GetAvailableFares>';
		 
		 return $xml;
	}
}